{extend name="layout/base" /}

{block name="style"}
<style type="text/css">
    /*.table>tbody>tr>td{padding: 3px 5px;}*/
</style>
{/block}

{block name="body"}
<div class="wrapper wrapper-content">

<div class="row animated fadeIn">
    <div class="col-xs-12">
        <div class="ibox box-success">
            <div class="ibox-title">
                <h5>管理组列表</h5>
            </div>
            <div class="panel-body ibox-content mainbox">
                <div class="row">
                    <div class="col-xs-3">
                        <input type="button" value="添加管理组" class="btn btn-primary addBtn" />
                    </div>
                </div>
                <table class="table table-striped table-bordered cs_table-hover m-t-sm">
                    <tr>
                        <th>管理组名</th>
                        <th>操作</th>
                    </tr>
                    {foreach $datas as $v}
                    <tr b-id="{$v['id']}" b-name="{$v['name']}">
                        <td>{$v['name']}</td>
                        <td>
                            <a href="javascript:;" class="editBtn">修改</a> |
                            <a href="{:Url('/sysgroup/rule', ['id'=>$v['id']])}">分配权限</a> |
                            <a href="javascript:;" class="delBtn">删除</a>
                        </td>
                    </tr>
                    {/foreach}
                </table>
            </div>
        </div>

    </div><!--col-md-->
</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
    <form action="" method="post">
    <div class="modal-header">
        <button type="button" class="close closeModal" aria-hidden="true"> &times;</button>
        <h4 class="modal-title" id="editModalLabel">管理组管理</h4>
    </div>
    <div class="modal-body">
        <table class="table table-striped table-bordered">
            <tr>
                <td width="20%">管理组名：</td>
                <td><input class="form-control input-medium" type="text" name="name" /></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="hidden" name="rid" />
                    <input type="button" value="确定" class="btn btn-primary doSubmit"/>
                    <button type="button" class="btn btn-danger closeModal">关闭</button>
                </td>
            </tr>
        </table>
    </div>
    </form>
</div>
</div>
</div><!--modal-->

</div><!--wrapper-->
{/block}

{block name="script"}
<!--公共JS-->
{include file="common/commjs" /}
<script type="text/javascript">

    $(function () {
        $('.mainbox .addBtn').on('click', function () {
            $('#editModal input[name="rid"]').val(0);
            $('#editModal').modal('show');
        });

        $('.mainbox .editBtn').on('click', function () {
            var dataPat = $(this).parents('tr:first');
            var rid = dataPat.attr('b-id');
            var name = dataPat.attr('b-name');

            $('#editModal input[name="rid"]').val(rid);
            $('#editModal input[name="name"]').val(name);

            $('#editModal').modal('show');
        });

        $('.mainbox .delBtn').on('click', function () {
            var dataPat = $(this).parents('tr:first');
            var rid = dataPat.attr('b-id');
            var parms = {
                'url': '{:Url("/sysgroup/del")}',
                'data': {'rid': rid}
            };
            var cbfun = function () {
                sendAjax(parms);
            };
            alertWarning('请谨慎操作,确定删除吗?', '', cbfun);
        });
        // 表单重置
        var formReset = function (former) {
            former[0].reset(); // former[0]转化成DOM对象
        };

        $('#editModal .doSubmit').on('click', function () {
            var former = $(this).parents('form:first');
            var selize = former.serialize();
            var rid = $('#editModal input[name="rid"]').val();
            if (rid == 0) {
                var parms = {
                    'url': '{:Url("/sysgroup/add")}',
                    'data': selize
                };
                sendAjax(parms);
                // $.when(sendAjax(parms)).done(function (data) {
                //     formReset(former);
                //     $('#editModal').modal('hide');
                // });
            } else {
                var parms ={
                    'url': '{:Url("/sysgroup/update")}',
                    'data': selize
                };
                sendAjax(parms);
                // $.when(sendAjax(parms)).done(function (data) {
                //     formReset(former);
                //     $('#editModal').modal('hide');
                // });
            }

        });

        $('#editModal .closeModal').on('click', function () {
            var former = $(this).parents('form:first');
            formReset(former);
            $('#editModal').modal('hide');
        });
    });
</script>
{/block}